package lee.study.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import javax.sound.sampled.*;
import java.beans.Encoder;
import java.io.*;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author licc
 * @create 2018-01-31 16:23
 * @desc
 **/
public class Test {
    public static void main(String[] args) throws Exception {
//        callTTS();
//        uploadFile();
        readExcel();
//        Date date = parseStringToDate("20180101", "124501");
//        System.out.println(date);
//        test();
//        getAudioDuration();
    }

    private static void callTTS() {
        String url = "http://192.168.0.161:8080/sdyy_service/TtsInsertCP";
        JSONObject params = new JSONObject();
        JSONObject list = new JSONObject();
        JSONArray param = new JSONArray();
        params.put("params", list);
        list.put("list", param);

        JSONObject param1 = new JSONObject();
        param1.put("channelCode", "ent");
        param1.put("id", "453");
        param1.put("mobile", 18511314141L);
        param1.put("date", 20180201);
        param1.put("time", 182000);
        param1.put("scriptName", "TTS.dat");
        param1.put("recordingName", "6.wav|7.wav|8.wav|9.wav|10.wav");
        param.add(param1);

        System.out.println(params.toJSONString());
        String res = HttpClientUtils.getInstance().doPostWithJsonResult(url, params.toJSONString());
        System.out.println(res);
    }

    private static void uploadFile() throws IOException {
        FTPUtils instance = FTPUtils.getInstance("admin", "admin", "172.16.103.189", 21 + "");
        instance.uploadFile("导出新生行为数据列表.xls",
                new FileInputStream(new File("G:\\导出新生行为数据列表.xls")), instance);
    }

    public static void readExcel() throws Exception {
        long l = System.currentTimeMillis();
        System.out.println(new Date());
        File file = new File("G:\\test1.xlsx");
        InputStream inputStream = new FileInputStream(file);
//        List<TestBean> testBeans = ExcelUtils.loadFromExcel(inputStream, TestBean.class);
        System.out.println(ExcelUtils.getExcelRowNum(inputStream,TestBean.class));
        long l1 = System.currentTimeMillis();
        System.out.println(new Date());
        System.out.println(l1-l);
    }

    private static Date parseStringToDate(String sdate, String stime) {
        StringBuilder stringBuilder = new StringBuilder();
        if (StringUtils.isBlank(sdate) || StringUtils.isBlank(stime)) {
            return null;
        }


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date parseDate = null;
        try {
            parseDate = simpleDateFormat.parse(sdate+stime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parseDate;
    }

    private static void test() {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 17; i++) {
            list.add(i);
        }


        if (list != null && list.size() > 0) {
            int SIZE = 3;
            int page = (list.size() + SIZE - 1) / SIZE;
            List<Integer> newtimelist;
            for (int i = 0; i < page; i++) {
                int start = i * SIZE;
                int end = i * SIZE + SIZE;
                end = (end > list.size()) ? list.size() : end;

                newtimelist = list.subList(start, end);
                System.out.println("~~~时间：" + newtimelist.get(0) + "~~~~~~~结束时间：" + newtimelist.get(newtimelist.size() - 1));
            }
        }
    }

    private static void getAudioDuration() throws Exception {
        File audio = new File("G:\\9.wav");
//        FileInputStream fin = new FileInputStream(audio);
//        AudioInputStream ain = AudioSystem.getAudioInputStream(fin);
//        AudioFormat format=ain.getFormat();
//        int frameSize = format.getFrameSize();
//        System.out.println(frameSize);

//        File file = new File("d:/test.wav");
//        Clip clip = AudioSystem.getClip();
//        AudioInputStream ais = AudioSystem.getAudioInputStream(audio);
//        clip.open(ais);
//        System.out.println( clip.getMicrosecondLength() / 1000000D + " s" );

        AudioInputStream stream = AudioSystem.getAudioInputStream(audio);
        System.out.println(getLength(stream));

    }

    public static double getLength(AudioInputStream stream) throws Exception {
//        AudioInputStream stream;
//        stream = AudioSystem.getAudioInputStream(new URL(path));
        AudioFormat format = stream.getFormat();
        if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format
                    .getSampleRate(), format.getSampleSizeInBits() * 2, format
                    .getChannels(), format.getFrameSize() * 2, format
                    .getFrameRate(), true); // big endian
            stream = AudioSystem.getAudioInputStream(format, stream);
        }
        DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(),
                ((int) stream.getFrameLength() * format.getFrameSize()));
        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.close();
        return clip.getBufferSize()
                / (clip.getFormat().getFrameSize() * clip.getFormat()
                .getFrameRate());
    }




}
